package com.xb.p42_touchevent;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnTouchListener, View.OnClickListener, View.OnLongClickListener {

    TestView testView1;
    TestView2 testView2_1;
    TestView2 testView2_2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        testView1=findViewById(R.id.testView1);
        testView2_1=findViewById(R.id.testView2_1);
        testView2_2=findViewById(R.id.testView2_2);


        testView2_1.setColor(Color.RED);
        testView2_2.setColor(Color.YELLOW);

        //点击
        testView1.setOnClickListener(this);
        testView2_1.setOnClickListener(this);
        testView2_2.setOnClickListener(this);

        //长按
        testView1.setOnLongClickListener(this);
        testView2_1.setOnLongClickListener(this);
        testView2_2.setOnLongClickListener(this);

        //触摸
        testView1.setOnTouchListener(this);
        testView2_1.setOnTouchListener(this);
        testView2_2.setOnTouchListener(this);

    }

    @Override
    public void onClick(View v) {
        Toast.makeText(this,"onClick...",Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onLongClick(View v) {
        Toast.makeText(this,"onLongClick...",Toast.LENGTH_SHORT).show();
        return false;//longClick之后会继续传递给onTouch，再传递给onClick
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Toast.makeText(this,v.getId()+"onTouch..."+event.getAction(),Toast.LENGTH_SHORT).show();
        return false;//false，当前事件继续往下传递，传递给onClick，如果自定义的View中重写了onTouchEvent，将继续往下传递给onTouchEvent；true，已经消化了当前事件，不再往下传递
    }
}